package xx.rabbitmq.conf;

import org.springframework.amqp.core.*;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class BackConfig {
    /**
     * 交换机名称
     */
    public static final String BACK_EXCHANGE = "backup.exchange";
    /**
     * 队列名称
     */
    public static final String BACK_QUEUE = "backup.queue";
    /**
     * 报警队列
     */
    public static final String WARN_QUEUE = "warning.queue";

    /**
     * 声明备份队列
     *
     * @return
     */
    @Bean("backupQueue")
    public Queue backupQueue() {
        return QueueBuilder.durable(BACK_QUEUE).build();
    }

    /**
     * 声明报警队列
     *
     * @return
     */
    @Bean("warningQueue")
    public Queue warningQueue() {
        return QueueBuilder.durable(WARN_QUEUE).build();
    }

    /**
     * 声明备份交换机
     * @return
     */
    @Bean("backupExchange")
    public FanoutExchange backupExchange() {
        return new FanoutExchange(BACK_EXCHANGE);
    }

    /**
     * 绑定 备份队列绑定备份交换机
     * @param backupQueue
     * @param backupExchange
     * @return
     */
    @Bean
    public Binding backupQueueBindingBackupExchange(@Qualifier("backupQueue") Queue backupQueue,
                                                    @Qualifier("backupExchange") FanoutExchange backupExchange){
        return BindingBuilder.bind(backupQueue).to(backupExchange);
    }

    /**
     * 绑定 报警队列绑定备份交换机
     *
     * @param warningQueue
     * @param backupExchange
     * @return
     */
    @Bean
    public Binding warningQueueBindingBackupExchange(@Qualifier("warningQueue") Queue warningQueue,
                                                     @Qualifier("backupExchange") FanoutExchange backupExchange){
        return BindingBuilder.bind(warningQueue).to(backupExchange);
    }

}
